package com.huikeportal.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.huike.model.MsgResponse;
import com.huike.tools.DateTool;
import com.huike.tools.MessageTool;
import com.huikeportal.model.MenuInfo;
import com.huikeportal.model.MessageCommon;
import com.huikeportal.model.RoleInfo;
import com.huikeportal.model.RolePageButton;
import com.huikeportal.model.TreeHtml;
import com.huikeportal.model.UserInfo;
import com.huikeportal.model.common.InterFaceModel;
import com.huikeportal.service.InterFaceCommon;
import com.huikeportal.service.LoginService;
import com.huikeportal.service.RoleApiService;
import com.huikeportal.service.RoleService;

@Controller
public class RoleAPI {

	@Autowired
	private InterFaceCommon interFaceCommon;

	@Autowired
	private RoleApiService roleApiService;

	@Autowired
	private RoleService roleService;

	@Autowired
	private LoginService loginService;

	/**
	 * 获取当前用户的角色树Html
	 * 
	 * @param userId
	 * @param selectedRoleId
	 * @return
	 */
	@RequestMapping(value = "/huike_getRoleTreeHtml", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getRoleTreeHtml(String userId, String selectedRoleId) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getRoleTreeHtml");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (userId == null || "".equals(userId) || selectedRoleId == null
				|| "".equals(selectedRoleId)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		try {
			String roleHtml = roleApiService.getRoleListHtml(userId,
					selectedRoleId);
			msgResponse.setObject(roleHtml);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 根据当前用户查询对应菜单按钮信息
	 * 
	 * @param userName
	 * @param source
	 * @return
	 */
	@RequestMapping(value = "/huike_getRoleButtonList", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getRoleButtonList(String userName, String roleId,
			String source) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getRoleButtonList");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (userName == null || "".equals(userName) || roleId == null
				|| "".equals(roleId) || source == null || "".equals(source)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		try {
			UserInfo user = new UserInfo();
			user.setUserName(userName);
			user.setSource(source);
			UserInfo userInfo = loginService.getUserInfo(user);
			List<RolePageButton> roleHtml = roleService.getButtonHtml(userInfo,
					roleId);
			// 将所有一级页面的id拼接成字符串传回页面
			String divString = "";
			// 获取所有一级页面
			List<MenuInfo> oneList = roleApiService.getPageInfo("1", roleHtml,
					userInfo.getSysLang());
			for (int i = 1; i < oneList.size() + 1; i++) {
				oneList.get(i - 1).setSortId(i);
				divString += oneList.get(i - 1).getMenuId() + ",";
			}
			divString = divString.substring(0, divString.length() - 1);
			// 获取所有二级页面
			List<MenuInfo> twoList = roleApiService.getPageInfo("2", roleHtml,
					userInfo.getSysLang());
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("oneList", oneList);
			map.put("twoList", twoList);
			map.put("divString", divString);
			map.put("roleHtml", roleHtml);
			msgResponse.setObject(map);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 获取当前角色的所有角色列表
	 * 
	 * @return
	 */
	@RequestMapping(value = "/huike_getRoleListById", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getRoleListById(String userName, String source) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getRoleListById");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (userName == null || "".equals(userName) || source == null
				|| "".equals(source)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		try {
			UserInfo userInfo = new UserInfo();
			userInfo.setUserName(userName);
			userInfo.setSource(source);
			userInfo = loginService.getUserInfo(userInfo);
			List<TreeHtml> roleList = roleService.getRoleList(userInfo);
			msgResponse.setObject(roleList);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 新增角色
	 * 
	 * @param userName
	 * @param source
	 * @param parentRoleId
	 * @param roleName
	 * @param roleDes
	 * @return
	 */
	@RequestMapping(value = "/huike_addRole", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String addRole(String userName, String source, String parentRoleId,
			String roleName, String roleDes) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getRoleListById");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (userName == null || "".equals(userName) || source == null
				|| "".equals(source) || parentRoleId == null
				|| "".equals(parentRoleId) || roleName == null
				|| "".equals(roleName)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		try {
			UserInfo userInfo = new UserInfo();
			RoleInfo roleInfo = new RoleInfo();
			userInfo.setUserName(userName);
			userInfo.setSource(source);
			roleInfo.setParentId(Long.parseLong(parentRoleId));
			roleInfo.setRoleName(roleName);
			roleInfo.setDescription(roleDes);
			String message = roleService.addRole(roleInfo, userInfo);
			msgResponse.setObject(message);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 根据角色id获取角色信息
	 * 
	 * @param roleId
	 * @return
	 */
	@RequestMapping(value = "/huike_getRoleInfoById", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String getRoleInfoById(String roleId) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_getRoleInfoById");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (roleId == null || "".equals(roleId)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		// 处理
		try {
			RoleInfo roleInfo = new RoleInfo();
			roleInfo.setRoleId(roleId);
			roleInfo = roleService.getRoleByRoleId(roleInfo);
			msgResponse.setObject(roleInfo);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 编辑角色
	 * 
	 * @return
	 */
	@RequestMapping(value = "/huike_editRole", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String editRole(String roleId, String parentId, String roleName,
			String roleDes) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_editRole");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (roleId == null || "".equals(roleId) || parentId == null
				|| "".equals(parentId) || roleName == null
				|| "".equals(roleName)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		try {
			RoleInfo roleInfo = new RoleInfo();
			roleInfo.setRoId(Long.parseLong(roleId));
			roleInfo.setRoleName(roleName);
			roleInfo.setDescription(roleDes);
			roleInfo.setParentId(Long.parseLong(parentId));
			String message = roleService.editRole(roleInfo);
			msgResponse.setObject(message);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 删除角色
	 * 
	 * @return
	 */
	@RequestMapping(value = "/huike_delRole", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String delRole(String roleId) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_delRole");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (roleId == null || "".equals(roleId)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		try {
			RoleInfo roleInfo = new RoleInfo();
			roleInfo.setRoleId(roleId);
			MessageCommon message = roleService.deleteRole(roleInfo);
			msgResponse.setObject(message);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}

	/**
	 * 保存角色菜单配置项
	 * 
	 * @return
	 */
	@RequestMapping(value = "/huike_saveRoleConfig", produces = { "application/json;charset=UTF-8" })
	@ResponseBody
	public String saveRoleConfig(String roleId, String buttonString) {
		// 接口记录对象
		InterFaceModel interFaceModel = new InterFaceModel();
		// 接口名称
		interFaceModel.setInterName("huike_saveRoleConfig");
		// 接口开始时间
		interFaceModel.setStartTime(DateTool.getDateYmdHms());
		// 返回对象
		MsgResponse msgResponse = new MsgResponse();
		// 校验参数
		if (roleId == null || "".equals(roleId) || buttonString == null
				|| "".equals(buttonString)) {
			msgResponse.setRespCode("90003");
			msgResponse.setRespCodeDes(MessageTool.RESP_90003);
			// 接口结束时间
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			// 接口状态
			interFaceModel.setInterStatus("success");
			// 记录接口调用情况
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
		try {
			RoleInfo roleInfo = new RoleInfo();
			roleInfo.setRoleId(roleId);
			roleInfo.setButtonString(buttonString);
			String message = roleService.saveConfig(roleInfo);
			msgResponse.setObject(message);
			msgResponse.setRespCode("90001");
			msgResponse.setRespCodeDes(MessageTool.RESP_90001);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("success");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		} catch (Exception e) {
			msgResponse.setRespCode("90002");
			msgResponse.setRespCodeDes(MessageTool.RESP_90002);
			interFaceModel.setEndTime(DateTool.getDateYmdHms());
			interFaceModel.setInterStatus("failure");
			interFaceCommon.logInterface(interFaceModel);
			return JSONObject.fromObject(msgResponse).toString();
		}
	}
}
